package DingDingMessage;

import cn.hutool.core.codec.Base64;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class Test {
    static String webHook = "https://oapi.dingtalk.com/robot/send?access_token=cba86a6a949ebd656e65efa150b790d4c80e491c522ae92319da259d190f71d4";

    public static void main(String[] args) {
        LocalDate a = LocalDate.now();

        LocalDateTime now = LocalDateTime.now();
        String begin = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String end = now.plusMinutes(3).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        System.out.println(begin);
        System.out.println(end);


        int[] arr = new int[]{1,5,6,0,7,4,9,3};
        int[] index = new int[]{0,1,2,3,4,0,5,1,2,6,7};
        String tel = "";
        for(int i:index){
            tel +=arr[i];
        }
        System.out.println(tel);


        System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy.MM.dd")));

        try {
            sendMsg("1","222");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
    }

    public static void sendMsg(String url, String name) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {

        //群机器人复制到的秘钥secret
        String secret = "SECb96714cf5888ca411a4d03314be69e388adae325242fb30ac35321b2eab42c9e";
        //获取系统时间戳
        Long timestamp = System.currentTimeMillis();
        //拼接
        String stringToSign = timestamp + "\n" + secret;
        //使用HmacSHA256算法计算签名
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
        //进行Base64 encode 得到最后的sign，可以拼接进url里
        String sign = URLEncoder.encode(new String(Base64.encode(signData)), "UTF-8");
        //钉钉机器人地址（配置机器人的webhook）
        String dingUrl = webHook+"&timestamp=" + timestamp + "&sign=" + sign;

        try {

            //是否通知所有人
            boolean isAtAll = false;
            //通知具体人的手机号码列表
            List<String> mobileList = new ArrayList<>();
            mobileList.add("13121091304");
            String userUrl = url;
            //钉钉机器人消息内容
            String content = "您的" + name + "域名" + userUrl + "已被微信限制";
            //组装请求内容
            String reqStr = buildReqStr(content, isAtAll, mobileList);
            //推送消息（http请求）
            String result = HttpUtil.post(dingUrl, reqStr);
            // String result = HttpClientUtil.sendPostDataByJson(sign, reqStr,"utf8");
            System.out.println("result == " + result);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 组装请求报文
     *
     * @param content
     * @return
     */
    private static String buildReqStr(String content, boolean isAtAll, List<String> mobileList) {
        //消息内容
        Map<String, String> contentMap = new HashMap<>();
        contentMap.put("content", content);
        //通知人
        Map<String, Object> atMap = new HashMap<>();
        //1.是否通知所有人
        atMap.put("isAtAll", isAtAll);
        //2.通知具体人的手机号码列表
        atMap.put("atMobiles", mobileList);
        atMap.put("atUsers", Arrays.asList(""));

        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("msgtype", "text");
        reqMap.put("text", contentMap);
        reqMap.put("at", atMap);

        return JSONUtil.toJsonStr(reqMap);
    }

    public static void getAlluser(){

    }
}

